<?php
namespace RMD\AccountBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use RMD\AccountBundle\Form\Type\SubscriberType;
use RMD\AccountBundle\Form\Type\ListsType;
use RMD\AccountBundle\Form\Type\GroupSetType;

use RMD\AccountBundle\Entity\Subscriber;
use RMD\AccountBundle\Entity\Lists;
use RMD\AccountBundle\Entity\GroupSet;

class ListsController extends Controller
{
	public function indexAction(){
		$user = $this->getUser();
		$lists = $user->getLists();
		

		return $this->render('RMDAccountBundle:Lists:index.html.php',
				array('lists' => $lists));
	}
	public function showAction($id){
		$list = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->find($id);
		if (!$list || $list->getUser()->getId() != $this->getUser()->getId()) {
			throw $this->createNotFoundException();
		}
		$form = $this->createForm(new SubscriberType()/*, $subscriber*/);
		return $this->render('RMDAccountBundle:Lists:show.html.php',
				array('form' => $form->createView(),
					'subscribers' => $list->getSubscribers(),
					'listid' => $list->getId()));
	}
	public function deleteAction($id){		
		/*$em = $this->getDoctrine()->getManager();
		$query = $em->createQuery(
			'DELETE FROM RMDAccountBundle:Subscriber s WHERE s.id = :id'
		)->setParameter('id', $id);

		$query->getResult();
		return $this->redirect($this->generateUrl('account_subscribers'));*/
	}
	public function createAction(){
		$list = new Lists();
		$form = $this->createForm(new ListsType(), $list);
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$form->bind($request);
			if ($form->isValid()) {
				$session = $this->get('session');
				$user = $this->getUser();
				//$user->addList($list);
				$list->setUser($user);
				$em = $this->getDoctrine()->getManager();
				$em->persist($list);
				$em->flush();
				$session->setFlash('success', 'List created');
				return $this->redirect($this->generateUrl('account_lists'));
			}
		}
		return $this->render('RMDAccountBundle:Lists:create.html.php',
				array('form' => $form->createView()));
	}
	public function editAction($listid){
		$doctrine = $this->getDoctrine();
		$list = $doctrine->getRepository('RMDAccountBundle:Lists')->find($listid);
		if (!$list || !$list->isOwnedBy($this->getUser())) {
			throw $this->createNotFoundException();
		}
		$form = $this->createForm(new ListsType(), $list);
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$form->bind($request);
			if ($form->isValid()) {
				$session = $this->get('session');
				$em = $doctrine->getManager();
				$em->persist($list);
				$em->flush();
				$session->setFlash('success', 'List modified');
				return $this->redirect($this->generateUrl('account_subscribers', array('listid' => $listid)));
			}
		}
		return $this->render('RMDAccountBundle:Lists:edit.html.php',
				array('form' => $form->createView(), 'listid' => $listid));
	}
	public function countSubscribersAction($listid){
		$res = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->countSubscribers($listid);
		return new Response($res);
	}
}
